package exttable.testfiller;

import exttable.SGTTestParametrs;
import exttable.view.ILocalsation;
import utils.Localisation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jan 29, 2010
 * Time: 11:45:06 AM
 * To change this template use File | Settings | File Templates.
 */
public class ValueVerifier extends InputVerifier implements ActionListener, ILocalsation {

    private String min;
    private String max;
    private boolean limitPresent;
    private String nameRu;
    private String nameEn;
    private String nameKz;
    private String componentName;

    public ValueVerifier(String min, String max, boolean limitPresent, String nameRu, String nameEn, String nameKz, String componentName) {
        this.min = min;
        this.max = max;
        this.limitPresent = limitPresent;
        this.nameRu = nameRu;
        this.nameEn = nameEn;
        this.nameKz = nameKz;
        this.componentName = componentName;
    }

    public String getNameRu() {
        return nameRu;
    }

    public String getNameEn() {
        return nameEn;
    }

    public String getNameKz() {
        return nameKz;
    }

    ValueVerifier(boolean limitPresent) {
        this.limitPresent = limitPresent;
    }

    @Override
    public boolean verify(JComponent input) {

        JTextField tf = (JTextField) input;
        if (tf.getText().length() > 0) {
            return IsNumber(tf);
        }
        if (tf.isEnabled()) {
            errorMessageTextEntered(Localisation.getMessage("error.nodata"));
            return false;
        } else {
            return false;
        }
    }


    @Override
    public void actionPerformed(ActionEvent e) {
    }

    private boolean IsNumber(JTextField textField) {
        try {
            if (limitPresent) {
                if (!(Double.parseDouble(textField.getText()) > Double.parseDouble(min) & Double.parseDouble(textField.getText()) < Double.parseDouble(max))) {
                    Component[] components = InputFormDisplay.resultPanel.getComponents();

                    boolean exceedLimits = exccedLimits();
                    SGTTestParametrs.getInstance().setExceedLimits(exceedLimits);
                    if (exceedLimits) {
                        for (Component res : components) {
                            if (res.getName().equals(componentName)) {
                                res.setForeground(Color.RED);
                            }
                        }
                    }
                    if (!exceedLimits) {
                        for (Component res : components) {
                            if (res.getName().equals(componentName))
                                res.setForeground(Color.BLACK);
                        }
                    }

                    return exceedLimits;
                }
            }
            if (!limitPresent) {
                Double.parseDouble(textField.getText());
                return true;
            }
        }
        catch (NumberFormatException ne) {
            errorMessageTextEntered(Localisation.getMessage("error.testentered"));
            textField.setText("");
            return false;
        }
        return true;
    }

    private void errorMessageTextEntered(String message) {
        JOptionPane.showMessageDialog(null, message, Localisation.getMessage("error.title"), JOptionPane.WARNING_MESSAGE);

    }

    private boolean exccedLimits() {
        Object[] options = {Localisation.getMessage("error.edit"), Localisation.getMessage("error.save")};
        int answer = JOptionPane.showOptionDialog(null, toString() + " " + Localisation.getMessage("error.between") + " " + min + " " + Localisation.getMessage("error.and") + " " + max, Localisation.getMessage("error.exceedtitle")
                , JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[1]);
        return answer != 0;
    }

    @Override
    public String toString() {
        return Localisation.getMsg(this);
    }
}
